<?php

namespace App\Http\Controllers\Home;

use App\Models\Home\User;
use App\Models\Home\UserInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;

class FollowController extends Controller
{

    //显示关注列表
    public function index($user_id)
    {   //取出用户信息
        $info = UserInfo::where('user_id', $user_id)->get()->first();
        //获取我的关注列表
        $follows = UserInfo::where('user_id', Session::get('user.id'))->get()->first()->following;
        $follows = explode(',', $follows);
        //取出该用户已关注的id列表，字符串格式
        $following = UserInfo::where('user_id', $user_id)
            ->get()->first()->following;
        //如果该用户还没有关注别人
        if(!$following){
            return view('home.profile', ['info' => $info, 'follows'=>$follows])->with('followingfails', '还没有关注其他用户');
        }else{
            //将关注列表转数组
            $following_array = explode(',', $following);
            //遍历出每个被关注用户的信息
            $follow = array();
            foreach($following_array as $following)
            {
                $follow[] = UserInfo::where('user_id',$following)->get()->first();
            }
            return view('home.profile' , ['info' => $info, 'follows'=>$follows, 'follow'=>$follow]);
        }
    }

    //关注其他用户
    public function update($user_id, $follow_id)
    {
        //本用户id
        $id = $user_id;
        //取出本用户已关注的id列表，字符串格式
        $following = UserInfo::where('user_id', $id)
                    ->get()->first()->following;
        //如果本用户还没有关注别人
        if(!$following){
            $following .= $follow_id;
            //把该用户id加入本用户的关注列表
            UserInfo::where('user_id', $id)->update(['following' => $following]);
        }else{
            //拼接该用户id
            $following .= ',' . $follow_id;
            //更新following字段
            UserInfo::where('user_id', $id)->update(['following' => $following]);
        }
        //在该用户的粉丝列表中加上本用户id
        $fans = UserInfo::where('user_id', $follow_id)->get()->first()->fans;
        //如果该用户还没有被关注
        if(!$fans){
            $fans .= $id;
            //把本用户id加入该用户的粉丝列表
            UserInfo::where('user_id', $follow_id)->update(['fans' => $fans]);
        }else{
            $fans .= ','.$id;
            UserInfo::where('user_id', $follow_id)->update(['fans' => $fans]);
        }
    }

    //取消关注
    public function destroy($user_id, $follow_id)
    {
        //将该用户id从本用户following字段中删除
        //本用户id
        $id = $user_id;
        //取出本用户已关注的id列表
        $following = UserInfo::where('user_id', $id)->get()->first()->following;
        //转数组
        $following_array = explode(',', $following);
        //该用户id在数组中的位置
        $key = array_search($follow_id, $following_array);
        //从数组中删除该用户id
        array_splice($following_array, $key, 1);
        //转字符串
        $following = implode(',', $following_array);
        //更新本用户的following字段
        UserInfo::where('user_id', $id)->update(['following' => $following]);

        //将本用户id从该用户fans字段中删除
        //取出该用户fans列表
        $fans = UserInfo::where('user_id', $follow_id)->get()->first()->fans;
        //转数组
        $fans_array = explode(',',$fans);
        //本用户id在数组中的位置
        $key = array_search($id, $fans_array);
        //从数组中删除本用户id
        array_splice($fans_array, $key, 1);
        //转字符串
        $fans = implode(',', $fans_array);
        //更新该用户的fans字段
        UserInfo::where('user_id', $follow_id)->update(['fans' => $fans]);
    }
}
